package controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.InvoiceDetailsInfo;
import myDaos.InvoiceDetailsDao;

@ManagedBean
@ViewScoped
public class InvoiceDetailsManagedBean {
	private String distributor;
	private List<InvoiceDetailsInfo> idlist;
	private Date from;
	private Date to;
	private String country;
	private boolean isMethodCalled = false;
	private List<SelectItem> distList;

	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	
	
	@EJB
	private InvoiceDetailsDao idd;

	
	
	
	
	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}

	


	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}


	public Date getFrom() {
		return from;
	}


	public void setFrom(Date from) {
		this.from = from;
	}


	public Date getTo() {
		return to;
	}


	public void setTo(Date to) {
		this.to = to;
	}


	public String getDistributor() {
		return distributor;
	}


	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}


	public List<InvoiceDetailsInfo> getIdlist() {
		return idlist;
	}


	public void setIdlist(List<InvoiceDetailsInfo> idlist) {
		this.idlist = idlist;
	}


	public boolean isMethodCalled() {
		return isMethodCalled;
	}


	public void setMethodCalled(boolean isMethodCalled) {
		this.isMethodCalled = isMethodCalled;
	}

	public void updateList()
	{
		idlist=idd.getInvoiceDetailsList(distributor,from,to);
		isMethodCalled = true;
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}
	
	@PostConstruct
	public void init(){
		to = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(to);
		gc.add(Calendar.DAY_OF_YEAR, -1);
		from = gc.getTime();
	}
}
